package com.example.demo.springceliemodel;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName LoginFactory
 * @Author lys4134
 * @Date 2022/03/04 10:30:41
 * @Description LoginFactory
 * @Version 1.0
 **/
@Component
public class LoginFactory implements ApplicationContextAware, InitializingBean {
    private static final Map<String, ILoginStrategy> ALL_LOGIN_TYPE_MAP = new ConcurrentHashMap<>();
    private ApplicationContext applicationContext;

    public ILoginStrategy getLoginHandle(String loginType) {
        return ALL_LOGIN_TYPE_MAP.get(loginType);
    }

    @Override
    public void afterPropertiesSet() {
        applicationContext.getBeansOfType(ILoginStrategy.class).values().forEach(handle -> {
            ALL_LOGIN_TYPE_MAP.put(handle.getLoginType(), handle);
        });
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
